package com.foodorder.repository;

import com.foodorder.entity.Review;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

import java.time.LocalDateTime;
import java.util.List;

/**
 * 评价仓库接口
 */
@Repository
public interface ReviewRepository extends JpaRepository<Review, Long>, JpaSpecificationExecutor<Review> {
    
    /**
     * 根据店铺ID查找评论
     */
    Page<Review> findByStoreIdOrderByCreatedAtDesc(Long storeId, Pageable pageable);
    
    /**
     * 根据用户ID查找评论
     */
    List<Review> findByUserIdOrderByCreatedAtDesc(Long userId);
    
    /**
     * 根据订单ID查找评论
     */
    List<Review> findByOrderId(Long orderId);
    
    /**
     * 根据店铺ID和评分查找评论
     */
    List<Review> findByStoreIdAndRating(Long storeId, Integer rating);
    
    /**
     * 统计店铺评论数量
     */
    long countByStoreId(Long storeId);
    
    /**
     * 统计用户评论数量
     */
    long countByUserId(Long userId);
    
    /**
     * 计算店铺平均评分
     */
    @Query("SELECT AVG(r.rating) FROM Review r WHERE r.storeId = :storeId")
    Double calculateAverageRating(@Param("storeId") Long storeId);
    
    /**
     * 根据评分统计店铺评论分布
     */
    @Query("SELECT r.rating, COUNT(r) FROM Review r WHERE r.storeId = :storeId GROUP BY r.rating")
    List<Object[]> getRatingDistribution(@Param("storeId") Long storeId);

    /**
     * 根据商品ID和显示状态统计评价数量
     */
    long countByProductIdAndDisplay(Long productId, Boolean display);

    /**
     * 根据商店ID和显示状态统计评价数量
     */
    long countByStoreIdAndDisplay(Long storeId, Boolean display);

    /**
     * 根据商品ID和评分区间统计评价数量
     */
    long countByProductIdAndRatingGreaterThanEqualAndDisplay(Long productId, Integer rating, Boolean display);

    /**
     * 根据商品ID和评分统计评价数量
     */
    long countByProductIdAndRatingAndDisplay(Long productId, Integer rating, Boolean display);

    /**
     * 根据商品ID和评分区间统计评价数量
     */
    long countByProductIdAndRatingLessThanEqualAndDisplay(Long productId, Integer rating, Boolean display);

    /**
     * 根据商店ID和评分区间统计评价数量
     */
    long countByStoreIdAndRatingGreaterThanEqualAndDisplay(Long storeId, Integer rating, Boolean display);

    /**
     * 根据商店ID和评分统计评价数量
     */
    long countByStoreIdAndRatingAndDisplay(Long storeId, Integer rating, Boolean display);

    /**
     * 根据商店ID和评分区间统计评价数量
     */
    long countByStoreIdAndRatingLessThanEqualAndDisplay(Long storeId, Integer rating, Boolean display);

    /**
     * 统计近期评价数量
     */
    long countByStoreIdAndCreatedAtAfterAndDisplay(Long storeId, LocalDateTime startTime, Boolean display);

    /**
     * 根据商品ID统计有图评价数量
     */
    @Query("SELECT COUNT(r) FROM Review r WHERE r.productId = :productId AND r.images IS NOT NULL AND r.images <> :emptyImages AND r.display = :display")
    long countByProductIdAndImagesNotNullAndImagesNotAndDisplay(
            @Param("productId") Long productId,
            @Param("emptyImages") String emptyImages,
            @Param("display") Boolean display);

    /**
     * 查询商品平均评分
     */
    @Query("SELECT AVG(r.rating) FROM Review r WHERE r.productId = :productId AND r.display = true")
    Double findAverageRatingByProductId(@Param("productId") Long productId);

    /**
     * 查询商店平均评分
     */
    @Query("SELECT AVG(r.rating) FROM Review r WHERE r.storeId = :storeId AND r.display = true")
    Double findAverageRatingByStoreId(@Param("storeId") Long storeId);
} 